package com.framework.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.TreeAgentDao;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.sys.SysAgentGrade;
import com.framework.entity.sys.SysTreeAgent;
import com.framework.exception.RRException;
import com.framework.exception.menu.RespCode;
import com.framework.service.AgentGradeService;
import com.framework.service.AgentService;
import com.framework.service.TreeAgentService;
import com.framework.view.AgentView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wq
 */
@Service("treeAgentService")
public class TreeAgentServiceImpl extends ServiceImpl<TreeAgentDao, SysTreeAgent> implements TreeAgentService {

	@Autowired
	private AgentService agentService;
	@Autowired
	private AgentGradeService agentGradeService;

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void insertTreeAgent(Long agentId) {
		if(agentId == null || agentId == 0){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		insertCycleAgent(agentId,agentId);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void insertCycleAgent(Long agentId, Long ownAgentId) {
		SysTreeAgent tree = new SysTreeAgent();
		if(agentId == null || agentId == 0){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		SysAgent agent = agentService.queryAgentById(agentId);
		if(agent != null){
			Long id = agent.getAgentId();
			Long pid = agent.getPid();
			tree.setAgentId(id);
			tree.setDeptId(agent.getDeptId());
			if(id.equals(ownAgentId) ){
				//需要添加自己
				tree.setIsOwn(2);
				tree.setSubId(ownAgentId);
			}else{
				tree.setIsOwn(1);
				tree.setSubId(ownAgentId);
			}
			baseMapper.insert(tree);
			if(pid != 0){
				//循环录入
				insertCycleAgent(pid,ownAgentId);
			}
		}
	}

	@Override
	public List<AgentView> querySubAgentList(String token,String grade) {
			SysAgent agent = agentService.queryAgentByToken(token);
			Long agentId = agent.getAgentId();
			List<AgentView> list = baseMapper.querySubAgentList(agentId,grade);
			AgentView a = new AgentView();
			a.setAgentId("0");
			a.setGrade(0);
			a.setName("全部");
			list.add(0,a);
			//添加自己
			SysAgentGrade g = agentGradeService.queryGradeByAgentId(agentId);
			AgentView a1 = new AgentView();
			a1.setAgentId(String.valueOf(agent.getAgentId()));
			a1.setGrade(g.getThisGrade());
			a1.setName(agent.getName());
			list.add(1,a1);
			return list;
	}

	@Override
	public boolean isAgentOwn(Long agentId, Long subAgentId) {
		SysTreeAgent treeAgent = new SysTreeAgent();
		treeAgent.setAgentId(agentId);
		treeAgent.setSubId(subAgentId);
		SysTreeAgent sysTreeAgent = baseMapper.selectOne(treeAgent);
        return sysTreeAgent != null;
	}

	@Override
	public List<Long> queryPidList(Long agentId, Long base) {

		return baseMapper.queryPidList(agentId,base);
	}

	@Override
	public List<AgentView> querySubAgent(String token) {
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		List<AgentView> list = baseMapper.querySubAgentList(agentId,"ASC");
		AgentView a = new AgentView();
		a.setAgentId("0");
		a.setGrade(0);
		a.setName("全部");
		list.add(0,a);
		//添加自己
		SysAgentGrade g = agentGradeService.queryGradeByAgentId(agentId);
		AgentView a1 = new AgentView();
		a1.setAgentId(String.valueOf(agent.getAgentId()));
		a1.setGrade(g.getThisGrade());
		a1.setName(agent.getName());
		list.add(1,a1);
		return list;
	}
}
